package smaragJava;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class AnalystDAO implements AnalystDAOInterface {

	Connector con;
	
	public AnalystDAO(Connector con){
		this.con = con;
	}
	
	/* (non-Javadoc)
	 * @see smaragJava.AnalystDAOInterface#insert(smaragJava.Analyst)
	 */
	public void insert(Analyst analyst) {
		String query;
		Statement s;
		String [] values = {analyst.getEmail(), analyst.getPassword(), analyst.getFname(), analyst.getLname(), analyst.getEducation()};
		
		query = con.abstractInsert("analyst", values);
		try{
			s = con.getConnection().createStatement();
			s.executeUpdate(query);
			s.close();
		}
		catch(SQLException e){
			e.printStackTrace();
		}
	}

	// throws CustomerDAOException;

	/* (non-Javadoc)
	 * @see smaragJava.AnalystDAOInterface#update(smaragJava.Analyst)
	 */
	public void update(Analyst analyst) {
		String query;
		Statement s;
		String [] values = {analyst.getEmail(), analyst.getPassword(), analyst.getFname(), analyst.getLname(), analyst.getEducation()};
		
		query = con.abstractUpdate(values, analyst.getId(), "analyst");
		try{
			s = con.getConnection().createStatement();
			s.executeUpdate(query);
			s.close();
		}
		catch(SQLException e){
			e.printStackTrace();
		}
	}

	// throws CustomerDAOException;

	/* (non-Javadoc)
	 * @see smaragJava.AnalystDAOInterface#delete(java.lang.String)
	 */
	public void delete(String analystname/* , analyst ,id */) {
		String[] fields = { "*" };
		String[] analystnames = { analystname };
		String query;
		
		query = con.abstractDelete(fields, analystnames, "analystname", "analyst");
		
		throw new UnsupportedOperationException("Not yet implemented");
	}

	// throws CustomerDAOException;

	/* (non-Javadoc)
	 * @see smaragJava.AnalystDAOInterface#findAll()
	 */
	public Vector<Analyst> findAll() {
		String[] fields = { "*" };
		String query;
		Statement s;
		ResultSet rs;
		Vector<Analyst> vector = new Vector();

		
		query = con.abstractSelect("analyst");
		try{
			s = con.getConnection().createStatement();
			rs = s.executeQuery(query);
			
			while(rs.next()){
				Analyst analyst = new Analyst();
				
				analyst.setEmail(rs.getString("email"));
				analyst.setFname(rs.getString("fname"));
				analyst.setLname(rs.getString("lname"));
				analyst.setUsername(rs.getString("analystname"));
				vector.add(analyst);
				
				analyst = null;
			}
			
			s.close();
		}
		catch(SQLException e){
			e.printStackTrace();
		}
		// con.executeQuery(query);
		return vector;
	}

	// throws CustomerDAOException;

	/* (non-Javadoc)
	 * @see smaragJava.AnalystDAOInterface#findByPrimaryKey(java.lang.String)
	 */
	public Analyst findByPrimaryKey(String id) {
		Analyst analyst = null;
		Statement s;
		ResultSet rs;
		String query;
		
		String[] fields = { "*" };
		String[] ids = { id };

		query = con.abstractSelect(fields, ids, "id", "analyst");
		try{
			s = con.getConnection().createStatement();
			rs = s.executeQuery(query);
					
			analyst = new Analyst();
			
			analyst.setEmail(rs.getString("email"));
			analyst.setFname(rs.getString("fname"));
			analyst.setLname(rs.getString("lname"));
			analyst.setUsername(rs.getString("analystname"));						
			
			s.close();
		}
		catch(SQLException e){
			e.printStackTrace();
		}
		// con.executeQuer();
		return analyst;
	}

	// throws CustomerDAOException;

	/* (non-Javadoc)
	 * @see smaragJava.AnalystDAOInterface#findByanalystname(java.lang.String)
	 */
	public Analyst findByanalystname(String analystname) {
		
		Analyst analyst = null;
		Statement s;
		ResultSet rs;
		String query;
		
		String[] fields = { "*" };
		String[] analystnames = { analystname };
		
		query = con.abstractSelect(fields, analystnames, "analystname", "analyst");
		try{
			s = con.getConnection().createStatement();
			rs = s.executeQuery(query);
					
			analyst = new Analyst();
			
			analyst.setEmail(rs.getString("email"));
			analyst.setFname(rs.getString("fname"));
			analyst.setLname(rs.getString("lname"));
			analyst.setUsername(rs.getString("analystname"));						
			
			s.close();
		}
		catch(SQLException e){
			e.printStackTrace();
		}
		// con.executeQuer();
		return analyst;
	}
	
}
